package com.cysion.study.ui.personal

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cysion.study.ktbox.Box.context
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.ApiException
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.ProMoneyAdapter
import com.cysion.study.entity.Blog
import com.cysion.study.entity.CommentEntity
import com.cysion.study.presenter.ProMoneyPresenter
import com.cysion.study.ui.iview.ProMoneyView
import com.cysion.study.widget.ShowToast
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_promoney.*

/**
 * 推广收益
 * Created by chu on 2019-07-01.
 */
class ProMoneyActivity:BaseActivity(), ProMoneyView {



    //绑定presenter
    private val presenter by lazy {
        ProMoneyPresenter().apply {
            attach(this@ProMoneyActivity)
        }
    }
    private lateinit var proAdapter:ProMoneyAdapter
    private val mCarousels: MutableList<CommentEntity> = mutableListOf()
    private var curPage = 1

    override fun getLayoutId(): Int = R.layout.activity_promoney

    override fun initView() {
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("推广收益")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        initRefreshLayout()
        initRecyclerView()

    }
    //加载数据
    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }




    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            presenter.getProMoney(curPage)
            smartLayout.setEnableLoadMore(true)
            fl_load_state.visibility= View.GONE
        }
        smartLayout.setOnLoadMoreListener {
            fl_load_state.visibility= View.GONE
            presenter.getProMoney(curPage)
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        rvBloglist.isNestedScrollingEnabled = false
        proAdapter = ProMoneyAdapter(mCarousels, context)
        rvBloglist.adapter = proAdapter
        rvBloglist.layoutManager = LinearLayoutManager(context)

    }

    //回调接口数据  收益
    override fun onProMoney(it: MutableList<Blog>?) {


    }

    //出错回调
    override fun onGetProMoneyError(handle: ApiException) {
        fl_load_state.visibility=View.VISIBLE
        tvLoadFinish.visibility=View.VISIBLE
    }

    override fun loading() {
        Alert.loading(this)
    }

    override fun stopLoad() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
        Alert.close()
    }

    override fun error(code: Int, msg: String) {
        ShowToast(msg)
    }
    override fun closeMvp() {
        presenter.detach()
    }
}